# 测试优化后的API功能
Write-Host "=== 测试优化后的API功能 ===" -ForegroundColor Cyan

# 设置基础URL
$BASE_URL = "http://localhost:8900/api"
$HEALTH_URL = "http://localhost:8900/health"

# 1. 测试健康检查
Write-Host "\n1. 测试健康检查..." -ForegroundColor Green
try {
    $healthResponse = Invoke-RestMethod -Uri $HEALTH_URL -Method Get -ContentType "application/json"
    Write-Host "✓ 健康检查成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $healthResponse -Depth 5)
} catch {
    Write-Host "✗ 健康检查失败: $_" -ForegroundColor Red
    exit 1
}

# 2. 获取用户列表（初始应该为空）
Write-Host "\n2. 获取用户列表..." -ForegroundColor Green
try {
    $usersList = Invoke-RestMethod -Uri "$BASE_URL/users" -Method Get -ContentType "application/json"
    Write-Host "✓ 获取用户列表成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $usersList -Depth 5)
} catch {
    Write-Host "✗ 获取用户列表失败: $_" -ForegroundColor Red
    exit 1
}

# 3. 创建用户
Write-Host "\n3. 创建用户..." -ForegroundColor Green
try {
    $newUser = @{
        name = "张三"
        age = 30
    } | ConvertTo-Json
    
    $createdUser = Invoke-RestMethod -Uri "$BASE_URL/users" -Method Post -Body $newUser -ContentType "application/json"
    Write-Host "✓ 创建用户成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $createdUser -Depth 5)
    
    # 保存用户ID用于后续测试
    $userId = $createdUser.data.id
} catch {
    Write-Host "✗ 创建用户失败: $_" -ForegroundColor Red
    exit 1
}

# 4. 获取用户详情
Write-Host "\n4. 获取用户详情..." -ForegroundColor Green
try {
    $userDetail = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method Get -ContentType "application/json"
    Write-Host "✓ 获取用户详情成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $userDetail -Depth 5)
} catch {
    Write-Host "✗ 获取用户详情失败: $_" -ForegroundColor Red
    exit 1
}

# 5. 更新用户
Write-Host "\n5. 更新用户..." -ForegroundColor Green
try {
    $updatedUser = @{
        name = "李四"
        age = 35
    } | ConvertTo-Json
    
    $result = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method Put -Body $updatedUser -ContentType "application/json"
    Write-Host "✓ 更新用户成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $result -Depth 5)
} catch {
    Write-Host "✗ 更新用户失败: $_" -ForegroundColor Red
    exit 1
}

# 6. 再次获取用户列表（应该有一个用户）
Write-Host "\n6. 再次获取用户列表..." -ForegroundColor Green
try {
    $usersList = Invoke-RestMethod -Uri "$BASE_URL/users" -Method Get -ContentType "application/json"
    Write-Host "✓ 获取用户列表成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $usersList -Depth 5)
} catch {
    Write-Host "✗ 获取用户列表失败: $_" -ForegroundColor Red
    exit 1
}

# 7. 删除用户
Write-Host "\n7. 删除用户..." -ForegroundColor Green
try {
    $deleteResult = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method Delete -ContentType "application/json"
    Write-Host "✓ 删除用户成功:" -ForegroundColor Green
    Write-Host (ConvertTo-Json $deleteResult -Depth 5)
} catch {
    Write-Host "✗ 删除用户失败: $_" -ForegroundColor Red
    exit 1
}

# 8. 验证删除结果
Write-Host "\n8. 验证删除结果..." -ForegroundColor Green
try {
    $response = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method Get -ContentType "application/json"
    Write-Host "✗ 删除验证失败: 用户仍然存在" -ForegroundColor Red
    exit 1
} catch {
    # 期望得到404错误
    Write-Host "✓ 删除验证成功: 用户不存在（404）" -ForegroundColor Green
}

Write-Host "\n=== 所有API测试完成！===" -ForegroundColor Green